<?php
class UserAction extends CommonAction {

    public function index() {
        $user = M('User');
        //搜索模块
        parent::search(array(
            'name'=>'用户名'
        ));
        if(!isset($_GET['act'])) {
            parent::page($user->count());
            $allUser = $user->field(array('id', 'name', 'reg_time', 'last_ip', 'last_time'))
                            ->order('reg_time DESC')
                            ->limit($this->limit)
                            ->select();
        } else if($_GET['act'] == 'search') {
            parent::page($user->where($this->map)->count());
            $allUser = $user->field(array('id', 'name', 'reg_time', 'last_ip', 'last_time'))
                            ->order('reg_time DESC')
                            ->where($this->map)
                            ->limit($this->limit)
                            ->select();
        }

        $this->assign(array(
            'alluser'=>$allUser,
            'empty'=>'<tr><td colspan="6">没有任何数据</td></tr>'
        ));

        $this->display();
    }

    public function update() {
        $id = $this->_get('id');
        if(isset($id)) {
            $user = M('User');
            $one = $user->field(array('score', 'account'))->where('id='.$id)->find();
            $this->assign('one', $one);

            if(isset($_POST['send'])) {
                $score = $this->_post('score');
                $account = $this->_post('account');
                if($score == '' || $account == '') $this->error('分数和金币不能为空字符串!', 0);
                $data['score'] = $score;
                $data['account'] = $account;
                $user->where('id='.$id)->save($data) ? $this->success('修改用户成功!', __URL__) : $this->error('系统出错，修改用户失败!', 0);
            }

            $this->display();
        }
    }

    public function delete() {
        $id = $this->_get('id');
        if(isset($id)) {
            $user = M('User');
            if(!$user->where('id='.$id)->getField('id')) $this->error('不存在此评论!', 0);
            $map['id'] = $id;
            $user->where($map)->delete() ? $this->success('删除用户成功!', __URL__) : $this->error('系统出错，删除用户失败!', 0);
        }
    }

    public function mulDelete() {
        $ids = implode(',', $_POST['select']);
        if(!$ids) $this->error('请至少勾选一项!', 0);
        $user = M('User');
        $map['id'] = array('in', $ids);
        $user->where($map)->delete() ? $this->success('批量删除用户成功!', __URL__) : $this->error('系统出错，批量删除用户失败!', 0);

    }

}

?>